#pragma once

#include <windows.h>
#include <stdio.h>
#include <map>
#include <string>
#include <regex>
#include <vector>
using std::wstring;
using std::map;
using std::vector;

namespace SubtitlePlayerSpace
{
    class SubtitlePlayer
    {
        public:
            SubtitlePlayer(const LPCWSTR path, const LPCWSTR encoding, int& errorStatus);
            SubtitlePlayer();
            virtual ~SubtitlePlayer();
            static LPCWSTR GetLastErrorMessage();
            boolean IsEncodingAccepted(wstring encoding) const;

            long GetEndTime();
            int GetFirstSubSize();
            wstring GetFirstSub(long& endTime);
            wstring GetNextSub(long& endTime);
            wstring GetSubByTime(long& endTime);
        protected:
        private:
            enum RowType {
                OrderRow,
                TimeRow,
                TextRow,
                EmptyRow,
                UnknownRow
            };
            static wstring errorMessage;
            static const wstring supportedEncodings[];
            static const int supportedEncodingsCount;
            static const std::wregex timeRegex;
            //static const std::regex_constants::match_flag_type fl;
                    
            int ReadFileLines(int& processedLines);
            //int GetSubNumber(vector<wstring>::iterator linesIterator, vector<wstring>::const_iterator endIterator, int &lastLine, vector<wstring>::iterator &outIterator) const;
            int SkipEmpty(vector<wstring>::iterator, vector<wstring>::const_iterator, vector<wstring>::iterator &outIterator) const;
            int OpenSubtitles(LPCWSTR path);
            int SetEncoding(LPCWSTR encoding);
            RowType GetRowType(const wstring& row, RowType previous = UnknownRow);
            int ConvertTimeRowToMS(const wstring& timeRow, long& startTime, long& endTime);
            int GetMSFromString(const wstring& timeString, long& time);
            void InsertSubLine(long time, const wstring & currentSub);
            void PrintMap();

            map<long, wstring> subLines;
            map<long, wstring>::iterator nextSub;
            wstring encoding;
            wstring filePath;
            FILE *subFile;
    };
    
    size_t GetSizeOfFile(wstring filePath);
}
